<?php

namespace App\Http\Controllers;

use App\Models\Company;
use App\Models\CompanyCertification;
use App\Models\CompanyCertificationDetail;
use App\Models\Region;
use App\Utils\CommonUtil;
use App\Utils\OptionUtil;
use App\Utils\RegionUtil;
use Illuminate\Http\Request;

class CompanyController extends BaseController
{
    public function index(Request $request)
    {
        $list     = [];
        $paginate = Company::query()->paginate(10);

        /**
         * @var Company $value
         */
        foreach ($paginate->items() as $item) {
            $company = [];
            foreach ($item->attributesToArray() as $key => $value) {
                $company[CommonUtil::underscoreToCamelCase($key)] = $value;
            }

            $list[] = $company;
        }

        return self::responseJson([
            'list'  => $list,
            'pages' => $paginate->lastPage(),
            'total' => $paginate->total(),
        ]);
    }

    public function create(Request $request)
    {
        $posts = $request->post();

        $regionCode          = $posts['regionCode'];
        $regionCode          = count($regionCode) > 0 ? $regionCode[count($regionCode) - 1] : '';
        $posts['regionCode'] = $regionCode;

        $region              = Region::query()->where('code', $regionCode)->first();
        $posts['regionName'] = implode('/', RegionUtil::getFullName($region));

        $data = [];
        foreach ($posts as $key => $value) {
            $data[CommonUtil::camelCaseToUnderscore($key)] = $value;
        }

        $data['create_time'] = date('Y-m-d H:i:s');
        $data['modify_time'] = date('Y-m-d H:i:s');

        Company::query()->insert($data);

        return self::responseMsg();
    }

    public function edit(Request $request)
    {
        $id    = $request->route('id');
        $posts = $request->post();

        $regionCode          = $posts['regionCode'];
        $regionCode          = count($regionCode) > 0 ? $regionCode[count($regionCode) - 1] : '';
        $posts['regionCode'] = $regionCode;

        $region              = Region::query()->where('code', $regionCode)->first();
        $posts['regionName'] = implode('/', RegionUtil::getFullName($region));

        $data = [];
        foreach ($posts as $key => $value) {
            $data[CommonUtil::camelCaseToUnderscore($key)] = $value;
        }

        $data['modify_time'] = date('Y-m-d H:i:s');

        Company::query()->where('id', $id)->update($data);

        return self::responseMsg();
    }

    public function show(Request $request)
    {
        $id   = $request->route('id');
        $item = Company::query()->where('id', $id)->first();

        foreach ($item->attributesToArray() as $key => $value) {
            $company[CommonUtil::underscoreToCamelCase($key)] = $value;
        }

        $region = Region::query()->where("code", $item->region_code)->first();

        $company['regionCode'] = RegionUtil::getFullCode($region);

        return self::responseJson($company);
    }

    /**
     * 添加合同编号和来源
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCertification(Request $request)
    {
        $posts = $request->post();

        $data = [];
        foreach ($posts as $key => $value) {
            $data[CommonUtil::camelCaseToUnderscore($key)] = $value;
        }

        $data['create_time'] = date('Y-m-d H:i:s');
        $data['modify_time'] = date('Y-m-d H:i:s');

        CompanyCertification::query()->insert($data);

        return self::responseMsg();
    }

    public function getCertificationByCompany(Request $request)
    {
        $companyId = $request->get('companyId');
        $company   = Company::query()->find($companyId);
        $list      = $company->companyCertification()->select(['id', 'agreement_code', 'agreement_origin'])->get();

        $options = [];
        foreach ($list as $item) {
            $options[] = [
                'value' => $item->id,
                'label' => $item->agreement_code,
            ];
        }

        return self::responseJson(['list' => $options]);
    }

    public function addCertificationDetail(Request $request)
    {
        $posts = $request->post();

        $data = [];
        foreach ($posts as $key => $value) {
            $data[CommonUtil::camelCaseToUnderscore($key)] = $value;
        }

        $data['certification_name'] = OptionUtil::getOptionNameByKey(OptionUtil::OPTION_NAME_CERTIFICATION, $data['certification_code']);
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['modify_time'] = date('Y-m-d H:i:s');

        CompanyCertificationDetail::query()->insert($data);

        return self::responseMsg();
    }
}
